import "dotenv/config";
import { ChatOpenAI } from "@langchain/openai";
import { ChatMessageHistory } from "langchain/memory";
import { HumanMessage, AIMessage } from "@langchain/core/messages";
import {
  ChatPromptTemplate,
  MessagesPlaceholder,
} from "@langchain/core/prompts";
import {
  RunnableSequence,
  RunnableWithMessageHistory,
} from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";

const chatModel = new ChatOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  configuration: {
    baseURL: process.env.OPENAI_API_BASE,
  },
});

const summaryPrompt = ChatPromptTemplate.fromTemplate(`
Progressively summarize the lines of conversation provided, adding onto the previous summary returning a new summary

Current summary:
{summary}

New lines of conversation:
{new_lines}

New summary:
`);

const summaryChain = RunnableSequence.from([
  summaryPrompt,
  chatModel,
  new StringOutputParser(),
]);

const newSummary = await summaryChain.invoke({
  summary: "",
  new_lines: "I'm 18",
});

const newSummary1 = await summaryChain.invoke({
  summary: newSummary,
  new_lines: "I'm male",
});

// console.log(newSummary1);

const newSummary2 = await summaryChain.invoke({
  summary: newSummary1,
  new_lines: "我爱打篮球",
});

console.log(newSummary2);
